# -*- coding: utf-8 -*-
import cv2
import numpy as np

# 08图像梯度-Scharr算子
# 图像梯度-laplacian算子:对噪音更敏感，所以会和其他方法仪器使用效果更好
# 这两种算子对图像处理的效果更好
#三种算子的差异对比
def cv_show(img,name,writename):
    cv2.imshow(name,img)
    cv2.imwrite(writename,img)
    cv2.waitKey()
    cv2.destroyAllWindows()

img = cv2.imread('picture.png',0)
sobelx1 = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobelx2 = cv2.convertScaleAbs(sobelx1)
sobely1 = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobely2 = cv2.convertScaleAbs(sobely1)
sobelxy = cv2.addWeighted(sobelx2,0.5,sobely2,0.5,0)

scahrrx = cv2.Scharr(img,cv2.CV_64F,1,0)
scahrrx = cv2.convertScaleAbs(scahrrx)
scahrry = cv2.Scharr(img,cv2.CV_64F,0,1)
scahrry = cv2.convertScaleAbs(scahrry)
scahrrxy = cv2.addWeighted(scahrrx,0.5,scahrry,0.5,0)

laplacian = cv2.Laplacian(img,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)

res = np.hstack((img,sobelxy, scahrrxy,laplacian))
cv_show(res,'res','08-Comparison operator.png')